package com.rocky.project.action.model;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import com.opensymphony.webwork.ServletActionContext;
import com.rocky.project.action.base.BaseAction;
import com.rocky.project.model.admin.Area;
import com.rocky.project.util.WebUtil;

public class AreaAction extends BaseAction {
	private static final long serialVersionUID = -3510474656970657392L;

	private Area area;

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	@SuppressWarnings("unchecked")
	public String genTree() throws Exception {
		String queryHql = "SELECT m FROM Area m WHERE m.deleted = false  and m.parent is null order by  m.id asc";
		pageRecords = baseService.executeHqlNoParam(queryHql, 0, null);

		// ServletActionContext.getResponse().setContentType("text/plain
		// charset=utf-8");
		// ServletActionContext.getResponse().getWriter().write(JSONArray.fromObject(pageRecords,
		// jsonConfig).toString());
		log.info("##AreaAction!genTree");
		return "area_tree";
	}

	public String read() throws Exception {
		java.util.List<Object> paramsvalues = new LinkedList<Object>();
		String queryHql = "SELECT m FROM Area AS m WHERE m.deleted = false   ";
		String keywordFilterValue = WebUtil.getParaValue("keywordFilterValue");
		String id = WebUtil.getParaValue("id");
		if (keywordFilterValue != null && !keywordFilterValue.equals("")) {
			queryHql += "    and (m.keyword like ? or m.title like ?  ) ";
			paramsvalues.add("%" + keywordFilterValue + "%");
			paramsvalues.add("%" + keywordFilterValue + "%");
		}
		if (id != null && !id.equals("")) {
			queryHql += "    and m.id = ?   ";
			paramsvalues.add(Long.valueOf(id));
		}
		if(StringUtils.isNotEmpty(this.from)&&this.from.equals("notarchivedarea")){
			queryHql += "  and m.parent is null and m.snapTime is null   ";
		}
		queryHql = queryHql + "    order by m.id asc";
		this.pageRecords = baseService.executeHqlWithParam(queryHql, paramsvalues.toArray(), getStart(), getPageSize());
		this.rowCount = baseService.getCountHqlWithParam(queryHql, paramsvalues.toArray());
		this.ajaxResultMap.put("data", this.pageRecords);
		this.ajaxResultMap.put("rowCount", this.rowCount);
		log.info("AreaAction:index.action->" + this.rowCount);
		return ajaxResult;
	}	


	@Override
	public String createProcess() throws Exception {

		JSONObject obj = JSONObject.fromObject(this.getJsondata());
		String parentId = obj.getString("parentId");
		obj.remove("parentId");
		obj.remove("parentName");
		area = (Area) JSONObject.toBean(obj, Area.class);
		if (parentId != null && !parentId.equals("") && !parentId.equals("-1")) {
			Area parent = new Area();
			parent.setId(Long.valueOf(parentId));
			area.setParent(parent);
		}
		WebUtil.init4create(area);
		baseService.create(area);

		return ajaxResult;
	}

	@Override
	public String updateProcess() throws Exception {
		JSONObject obj = JSONObject.fromObject(this.jsondata);
		String parentId = obj.getString("parentId");
		obj.remove("parentId");
		obj.remove("parentName");
		area = (Area) JSONObject.toBean(obj, Area.class);
		Area objDb = (Area) baseService.get(Area.class, area.getId());
//		String  json = FreemarkerUtil.getInstance().doExecute("/template/area.ftl", ActionContext.getContext().getActionInvocation());
//		log.info("json:\n"+json);
//		return NONE;

		BeanUtils.copyProperties(objDb, area);
		WebUtil.init4modify(objDb);
		if (parentId != null && !parentId.equals("") && !parentId.equals("-1")) {
			Area parentDB = (Area) baseService.get(Area.class, Long.valueOf(parentId));
			List<Long> pids = new ArrayList<Long>();
			pids.add(parentDB.getId());
			Area parent = parentDB.getParent();
			while (parent != null) {
				pids.add(parent.getId());
				parent = parent.getParent();
			}
			if (pids.contains(objDb.getId())) {
				this.addActionMessage("不能选择自己或子目录作为上级目录!");
				return ajaxResult;
			}
			objDb.setParent(parentDB);
		} else {
			objDb.setParent(null);
		}
		baseService.merge(objDb);
		return ajaxResult;
	}

	@Override
	public String deleteProcess() throws Exception {
		baseService.delete(baseService.get(Area.class, Long.valueOf(this.id)));
		ServletActionContext.getResponse().setContentType("text/plain charset=utf-8");
		ServletActionContext.getResponse().getWriter().write("{success:true}");
		return super.deleteProcess();
	}

}